<html>
<head><meta charset="utf-8"><title>Panic message from `catch_unwind`’s `dyn Any` · t-libs · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/index.html">t-libs</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Panic.20message.20from.20.60catch_unwind.60.E2.80.99s.20.60dyn.20Any.60.html">Panic message from `catch_unwind`’s `dyn Any`</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="245307886"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Panic%20message%20from%20%60catch_unwind%60%E2%80%99s%20%60dyn%20Any%60/near/245307886" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Simon Sapin <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Panic.20message.20from.20.60catch_unwind.60.E2.80.99s.20.60dyn.20Any.60.html#245307886">(Jul 08 2021 at 12:48)</a>:</h4>
<p>When catching a <code>panic!("using {}", "fmt")</code> with <code>catch_unwind</code>, is there any way of recovering the formatted panic message? I have a recollection of doing <code>.downcast_ref::&lt;String&gt;()</code> and <code>.downcast_ref::&lt;&amp;'static str&gt;()</code>, but these days they both return <code>None</code>, it looks like because the payload behind <code>dyn Any</code> is actually a private struct. Has that changed? CC <span class="user-mention" data-user-id="310399">@Mara</span> who worked on panic_2021</p>



<a name="245308303"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Panic%20message%20from%20%60catch_unwind%60%E2%80%99s%20%60dyn%20Any%60/near/245308303" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Simon Sapin <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Panic.20message.20from.20.60catch_unwind.60.E2.80.99s.20.60dyn.20Any.60.html#245308303">(Jul 08 2021 at 12:52)</a>:</h4>
<p>Hmm I read that wrong. <a href="https://github.com/rust-lang/rust/blob/0cd0709f19d316c4796fa71c5f52c8612a5f3771/library/std/src/panicking.rs#L492-L493">https://github.com/rust-lang/rust/blob/0cd0709f19d316c4796fa71c5f52c8612a5f3771/library/std/src/panicking.rs#L492-L493</a> looks like it still unsizes <code>String</code> to <code>dyn Any</code></p>



<a name="245309121"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Panic%20message%20from%20%60catch_unwind%60%E2%80%99s%20%60dyn%20Any%60/near/245309121" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mara <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Panic.20message.20from.20.60catch_unwind.60.E2.80.99s.20.60dyn.20Any.60.html#245309121">(Jul 08 2021 at 13:00)</a>:</h4>
<p>That should be a <code>String</code> right now, and might become a <code>&amp;'static str</code> once we teach the compiler a bit more about formatting constants.</p>



<a name="245323707"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Panic%20message%20from%20%60catch_unwind%60%E2%80%99s%20%60dyn%20Any%60/near/245323707" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Panic.20message.20from.20.60catch_unwind.60.E2.80.99s.20.60dyn.20Any.60.html#245323707">(Jul 08 2021 at 14:47)</a>:</h4>
<p>Downcasting<code>String</code> and <code>&amp;'static str</code> should cover all cases currently generated by <code>panic!</code>.</p>



<a name="245343221"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Panic%20message%20from%20%60catch_unwind%60%E2%80%99s%20%60dyn%20Any%60/near/245343221" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Simon Sapin <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Panic.20message.20from.20.60catch_unwind.60.E2.80.99s.20.60dyn.20Any.60.html#245343221">(Jul 08 2021 at 17:16)</a>:</h4>
<p>Yes that’s what I expected, but I have a case where the <code>Box&lt;dyn Any&gt;</code> returned by <code>catch_unwind</code> where downcasting returns <code>None</code> for both <code>String</code> and <code>&amp;'static str</code>: <a href="https://github.com/dgrunwald/rust-cpython/pull/264">https://github.com/dgrunwald/rust-cpython/pull/264</a></p>



<a name="245343529"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Panic%20message%20from%20%60catch_unwind%60%E2%80%99s%20%60dyn%20Any%60/near/245343529" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Gus Wynn <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Panic.20message.20from.20.60catch_unwind.60.E2.80.99s.20.60dyn.20Any.60.html#245343529">(Jul 08 2021 at 17:18)</a>:</h4>
<p>is it coercing the Box&lt;dyn Any&gt; into a <code>&amp;dyn Any</code>? maybe you need to &amp;*err when passing to handle_error?</p>
<p>i have had this problem before, but my default cause was not None, it was the string <code>Box&lt;dyn Any&gt;</code> like how the rust rt does it</p>



<a name="245344841"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Panic%20message%20from%20%60catch_unwind%60%E2%80%99s%20%60dyn%20Any%60/near/245344841" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Gus Wynn <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Panic.20message.20from.20.60catch_unwind.60.E2.80.99s.20.60dyn.20Any.60.html#245344841">(Jul 08 2021 at 17:29)</a>:</h4>
<p>on L230, sorry i wasnt specific</p>



<a name="245354865"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Panic%20message%20from%20%60catch_unwind%60%E2%80%99s%20%60dyn%20Any%60/near/245354865" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Panic.20message.20from.20.60catch_unwind.60.E2.80.99s.20.60dyn.20Any.60.html#245354865">(Jul 08 2021 at 18:47)</a>:</h4>
<p><span class="user-mention silent" data-user-id="219747">Simon Sapin</span> <a href="#narrow/stream/219381-t-libs/topic/Panic.20message.20from.20.60catch_unwind.60.E2.80.99s.20.60dyn.20Any.60/near/245343221">said</a>:</p>
<blockquote>
<p>Yes that’s what I expected, but I have a case where the <code>Box&lt;dyn Any&gt;</code> returned by <code>catch_unwind</code> where downcasting returns <code>None</code> for both <code>String</code> and <code>&amp;'static str</code>: <a href="https://github.com/dgrunwald/rust-cpython/pull/264">https://github.com/dgrunwald/rust-cpython/pull/264</a></p>
</blockquote>
<p>Sometimes it can be another <code>Box&lt;dyn Any&gt;</code> that contains the real payload. I don't know why this happens sometimes, but I've seen it also. (It took inspecting in the debugger to find out).  Here's the code I ended up using to handle it <a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=7f5a946f77790508e66f5241322bbcef">https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=7f5a946f77790508e66f5241322bbcef</a></p>



<a name="245355065"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Panic%20message%20from%20%60catch_unwind%60%E2%80%99s%20%60dyn%20Any%60/near/245355065" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Panic.20message.20from.20.60catch_unwind.60.E2.80.99s.20.60dyn.20Any.60.html#245355065">(Jul 08 2021 at 18:49)</a>:</h4>
<p>It recursively follows the box up to a depth limit. I don't think that's needed in practice, but my initial version of that function recursed infinitely due to this issue <a href="https://doc.rust-lang.org/std/any/index.html#smart-pointers-and-dyn-any">https://doc.rust-lang.org/std/any/index.html#smart-pointers-and-dyn-any</a>), and after I fixed that, it didn't seem like it was bad to have that guard there, so I left it.</p>



<a name="245355263"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Panic%20message%20from%20%60catch_unwind%60%E2%80%99s%20%60dyn%20Any%60/near/245355263" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Gus Wynn <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Panic.20message.20from.20.60catch_unwind.60.E2.80.99s.20.60dyn.20Any.60.html#245355263">(Jul 08 2021 at 18:51)</a>:</h4>
<p>I think you just need to dereference the box: <a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=4bcc90171a8a07a3667af9b6214e2786">https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=4bcc90171a8a07a3667af9b6214e2786</a><br>
as the the <code>dyn Any</code> in the <code>Box&lt;dyn Any&gt;</code> that catch_unwind gives you is the payload, but <code>Box</code> itself also implement <code>dyn Any</code></p>



<a name="245355282"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Panic%20message%20from%20%60catch_unwind%60%E2%80%99s%20%60dyn%20Any%60/near/245355282" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Panic.20message.20from.20.60catch_unwind.60.E2.80.99s.20.60dyn.20Any.60.html#245355282">(Jul 08 2021 at 18:51)</a>:</h4>
<p>(The included test will demonstrate the infinite recursion if you change <code>&amp;**v</code> to <code>v</code> or <code>&amp;*v</code> on line 22. it's stupidly subtle)</p>



<a name="245355449"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Panic%20message%20from%20%60catch_unwind%60%E2%80%99s%20%60dyn%20Any%60/near/245355449" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Panic.20message.20from.20.60catch_unwind.60.E2.80.99s.20.60dyn.20Any.60.html#245355449">(Jul 08 2021 at 18:52)</a>:</h4>
<p><span class="user-mention silent" data-user-id="257428">Gus Wynn</span> <a href="#narrow/stream/219381-t-libs/topic/Panic.20message.20from.20.60catch_unwind.60.E2.80.99s.20.60dyn.20Any.60/near/245355263">said</a>:</p>
<blockquote>
<p>I think you just need to dereference the box: <a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=4bcc90171a8a07a3667af9b6214e2786">https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=4bcc90171a8a07a3667af9b6214e2786</a><br>
as the the <code>dyn Any</code> in the <code>Box&lt;dyn Any&gt;</code> that catch_unwind gives you is the payload, but <code>Box</code> itself also implement <code>dyn Any</code></p>
</blockquote>
<p>That doesn't help in the case where the thing inside the box is itself a Box&lt;dyn Any&gt;, which I've seen (i think it was with thread join(), not catch_unwind, thogh)</p>



<a name="245355611"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Panic%20message%20from%20%60catch_unwind%60%E2%80%99s%20%60dyn%20Any%60/near/245355611" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Gus Wynn <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Panic.20message.20from.20.60catch_unwind.60.E2.80.99s.20.60dyn.20Any.60.html#245355611">(Jul 08 2021 at 18:53)</a>:</h4>
<p>thats very curious, I would be interested to see the code that caused that, just remembering the dereference the box before downcasting has always worked for me</p>



<a name="245355720"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Panic%20message%20from%20%60catch_unwind%60%E2%80%99s%20%60dyn%20Any%60/near/245355720" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Panic.20message.20from.20.60catch_unwind.60.E2.80.99s.20.60dyn.20Any.60.html#245355720">(Jul 08 2021 at 18:54)</a>:</h4>
<p>yeah, i wasn't sure what caused it either. i had to examine it in the debugger before i figured out that's what had happened.</p>



<a name="245355961"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Panic%20message%20from%20%60catch_unwind%60%E2%80%99s%20%60dyn%20Any%60/near/245355961" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Gus Wynn <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Panic.20message.20from.20.60catch_unwind.60.E2.80.99s.20.60dyn.20Any.60.html#245355961">(Jul 08 2021 at 18:56)</a>:</h4>
<p>I think in <span class="user-mention" data-user-id="219747">@Simon Sapin</span>'s pr, the call to extract_panic can just do *the_boxed_dyn_any as its just doing catch_unwind, but the recursive solution is an interesting one Ill remember if something weird ever comes up for me</p>



<a name="245356459"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Panic%20message%20from%20%60catch_unwind%60%E2%80%99s%20%60dyn%20Any%60/near/245356459" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Simon Sapin <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Panic.20message.20from.20.60catch_unwind.60.E2.80.99s.20.60dyn.20Any.60.html#245356459">(Jul 08 2021 at 19:00)</a>:</h4>
<p>Good catch <span class="user-mention" data-user-id="257428">@Gus Wynn</span>, thanks!</p>



<a name="245356604"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Panic%20message%20from%20%60catch_unwind%60%E2%80%99s%20%60dyn%20Any%60/near/245356604" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Simon Sapin <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Panic.20message.20from.20.60catch_unwind.60.E2.80.99s.20.60dyn.20Any.60.html#245356604">(Jul 08 2021 at 19:00)</a>:</h4>
<p>I think I’ve even encountered this bug in a different code base before, but it didn’t occur to me at all this time before you pointed it out</p>



<a name="245356723"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Panic%20message%20from%20%60catch_unwind%60%E2%80%99s%20%60dyn%20Any%60/near/245356723" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Simon Sapin <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Panic.20message.20from.20.60catch_unwind.60.E2.80.99s.20.60dyn.20Any.60.html#245356723">(Jul 08 2021 at 19:01)</a>:</h4>
<p>I’ve changed the parameter to <code>Box&lt;dyn Any&gt;</code> all the way, to avoid the subtlety</p>



<a name="245357062"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Panic%20message%20from%20%60catch_unwind%60%E2%80%99s%20%60dyn%20Any%60/near/245357062" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Simon Sapin <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Panic.20message.20from.20.60catch_unwind.60.E2.80.99s.20.60dyn.20Any.60.html#245357062">(Jul 08 2021 at 19:04)</a>:</h4>
<p>Perhaps this could be a lint</p>



<a name="245357095"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Panic%20message%20from%20%60catch_unwind%60%E2%80%99s%20%60dyn%20Any%60/near/245357095" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Simon Sapin <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Panic.20message.20from.20.60catch_unwind.60.E2.80.99s.20.60dyn.20Any.60.html#245357095">(Jul 08 2021 at 19:05)</a>:</h4>
<p>warn when usizing and auto-deref would both work</p>



<a name="245375201"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Panic%20message%20from%20%60catch_unwind%60%E2%80%99s%20%60dyn%20Any%60/near/245375201" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Gus Wynn <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Panic.20message.20from.20.60catch_unwind.60.E2.80.99s.20.60dyn.20Any.60.html#245375201">(Jul 08 2021 at 21:33)</a>:</h4>
<p><span class="user-mention silent" data-user-id="219747">Simon Sapin</span> <a href="#narrow/stream/219381-t-libs/topic/Panic.20message.20from.20.60catch_unwind.60.E2.80.99s.20.60dyn.20Any.60/near/245356723">said</a>:</p>
<blockquote>
<p>I’ve changed the parameter to <code>Box&lt;dyn Any&gt;</code> all the way, to avoid the subtlety</p>
</blockquote>
<p>I'd be curious to see if this actually works, or if you need an explicit dereference somewhere always (I suspect you do, but not sure)</p>



<a name="245375266"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Panic%20message%20from%20%60catch_unwind%60%E2%80%99s%20%60dyn%20Any%60/near/245375266" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Gus Wynn <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Panic.20message.20from.20.60catch_unwind.60.E2.80.99s.20.60dyn.20Any.60.html#245375266">(Jul 08 2021 at 21:33)</a>:</h4>
<p>the link that <span class="user-mention" data-user-id="209168">@Thom Chiovoloni</span> linked (<a href="https://doc.rust-lang.org/std/any/index.html#smart-pointers-and-dyn-any">https://doc.rust-lang.org/std/any/index.html#smart-pointers-and-dyn-any</a>) is great, first ive ever seen that, it would be nice as a lint (for Any specifically, but also maybe for things like anyhow::Error that also downcast</p>



<a name="245534891"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Panic%20message%20from%20%60catch_unwind%60%E2%80%99s%20%60dyn%20Any%60/near/245534891" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Simon Sapin <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Panic.20message.20from.20.60catch_unwind.60.E2.80.99s.20.60dyn.20Any.60.html#245534891">(Jul 10 2021 at 07:14)</a>:</h4>
<p><code>Boy&lt;dyn Any&gt;</code> all the way does work in the updated <a href="https://github.com/dgrunwald/rust-cpython/pull/264">https://github.com/dgrunwald/rust-cpython/pull/264</a>. Presumably with auto-deref on the <code>downcast_ref</code> calls, which are defined on <code>dyn Any</code> not just <code>&amp;dyn Any</code>.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>